package org.mx.dal.entity;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Getter;
import lombok.Setter;

/**
 * Geo Shape Multi Polygon定义类
 * 
 * @author john.peng
 *
 */
@Getter
@Setter
public class GeoShapeMultiPolygon extends GeoShape {
	private static final Logger logger = LoggerFactory.getLogger(GeoShapeMultiPolygon.class);

	@JsonIgnore
	private List<GeoShapePolygon> polygons = new ArrayList<>();

	/**
	 * 默认的构造函数
	 */
	public GeoShapeMultiPolygon() {
		super();
		super.setType("MultiPolygon");
	}

	private void fillData() {
		if (polygons.isEmpty()) {
			if (logger.isWarnEnabled()) {
				logger.warn("The multi polygon is empty.");
			}
			super.setCoordinates(new double[0][0][0]);
		} else {
			double[][][] coordinates = new double[polygons.size()][][];
			for (int index = 0; index < polygons.size(); index++) {
				double[][] polygon = new double[polygons.get(index).getPoints().size()][2];
				for (int p = 0; p < polygons.get(index).getPoints().size(); p++) {
					polygon[index][0] = polygons.get(index).getPoints().get(p).getLon();
					polygon[index][1] = polygons.get(index).getPoints().get(p).getLat();
					coordinates[index] = polygon;
				}
			}
			super.setCoordinates(coordinates);
		}
	}

	/**
	 * 向Mutli Polygon中添加一个Polygon
	 * @param polygon Ploygon
	 * @return Multi Polygon
	 */
	public GeoShapeMultiPolygon addPolygon(GeoShapePolygon polygon) {
		polygons.add(polygon);
		fillData();
		return this;
	}

	/**
	 * 向Multi Polygon中添加几个Polygon
	 * @param polygons Ploygon集合
	 * @return Multi Polygon
	 */
	public GeoShapeMultiPolygon addPolygons(List<GeoShapePolygon> polygons) {
		this.polygons.addAll(polygons);
		fillData();
		return this;
	}

	/**
	 * 清除Multi Polygon的Polygon
	 * @return Multi Polygon
	 */
	public GeoShapeMultiPolygon clear() {
		polygons.clear();
		fillData();
		return this;
	}
}
